<script>
import { mapWritableState, mapActions } from 'pinia'
import { User, Lock } from '@element-plus/icons-vue'
import { useAuthStore } from '@/stores/authStore.js'
import userRules from '@/rules/userRules.js'
import { login } from '@/api/authApi.js'

// import axios from 'axios'

export default {
  components: {
    User,
    Lock,
  },
  data() {
    return {
      user: {
        username: '',
        password: '',
        usertype: '1',
      },
      userRules: userRules,
      // {
      //   username: [
      //     { required: true, message: '用户名不能为空', trigger: 'blur' },
      //     { min: 2, message: '至少两位', trigger: 'blur' },
      //   ],
      //   password: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
      // },
    }
  },
  computed: {
    ...mapWritableState(useAuthStore, ['currentUser', 'menus', 'token']),
  },
  methods: {
    ...mapActions(useAuthStore, ['clearStore']),
    async doLogin() {
      //先把数据重置，避免缓存影响
      this.clearStore()
      // this.currentUser = null
      // this.token = null
      // this.menus = []
      //数据校验
      await this.$refs.loginForm.validate()
      //发送请求
      //放置于authApi.js中,最终置于util/request.js中
      // let result = await axios.post('http://127.0.0.1:3000/api/v1/login', this.user)
      // console.log(result.data.data.user)
      // console.log(result.data.data.token)
      // this.currentUser = result.data.data.user
      // this.token = result.data.data.token
      //登陆成功

      let data = await login(this.user)
      this.currentUser = data.user
      this.token = data.token

      this.$router.push({
        name: 'home',
      })
    },
  },
}
</script>

<template>
  <div class="box">
    <el-card>
      <template #header>教务管理系统</template>
      <el-form :model="user" :rules="userRules" ref="loginForm">
        <el-form-item prop="username">
          <el-input placeholder="学号/工号/用户名" size="large" v-model="user.username">
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="密码"
            size="large"
            type="password"
            show-password
            v-model="user.password"
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-radio-group v-model="user.usertype">
            <el-radio-button label="学生" value="3" />
            <el-radio-button label="教师" value="2" />
            <el-radio-button label="管理员" value="1" />
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="large" @click="doLogin()">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
.box {
  width: 100%;
  height: 100%;
  background-image: url('@/assets/img/login-background.jpg');
  background-size: 100% 100%;
  background-repeat: no-repeat;
  display: flex;
  align-items: center;
  justify-content: center;
}
.el-card {
  width: 380px;
}
.el-radio-group {
  width: 100%;
}
.el-radio-group :deep(.el-radio-button) {
  flex-grow: 1;
}
.el-radio-group :deep(.el-radio-button .el-radio-button__inner) {
  width: 100%;
}
.el-button {
  width: 100%;
}
</style>
